<?php

class Kpl_Filter_ReportFilename implements Zend_Filter_Interface
{
	protected $_mask;
	
	protected $_timestamp = false;
	
	protected $_maskMap = array(  0 => 'FunctionName'
								, 1 => 'Param'
								, 2 => 'Username'
								, 3 => 'UserCode'
								, 4 => 'Tty'
								, 5 => 'Host'
								, 6 => 'Month'
								, 7 => 'Day'
								, 8 => 'Hour'
								, 9 => 'Minute'
								, 10 => 'CurrentDate'
								, 11 => 'CurrentTime'
								, 12 => 'Dummy'
								, 13 => 'Dummy'
								, 14 => 'Dummy'
								, 15 => 'Dummy'
								, 16 => 'Dummy'
								);
	
	public function __construct($mask = false, $context = false)
	{
		$this->setMask($mask);
		$this->_context = $context;
	}
	
	public function setMask($mask)
	{
		$this->_mask = str_pad($mask,11,"N");
		return $this;
	}
	
	public function setContext($context)
	{
		$this->_context = $context;
		return $this;
	}
	
	protected function _isPartNeedToBeIncluded($positionInMap)
	{
		return ($this->_mask[$positionInMap] == 'Y');		
	}
	
	public function filter($name)
	{
		$this->_timestamp = ($this->_context['timestamp']) ? $this->_context['timestamp'] : time();

		for($positionInMap = 0; $positionInMap < strlen($this->_mask); $positionInMap++)
		{
			
			if($this->_isPartNeedToBeIncluded($positionInMap))
			{
				$part = $this->_maskMap[$positionInMap];
				$method = '_apply' . $part . 'FilterRule'; 
				$name = $this->$method($name);
			}
		}
		return $name;
	}
	
	protected function _applyDummyFilterRule($name)
	{
		return $name;
	}
	
	protected function _applyFunctionNameFilterRule($name)
	{
		return $name . '_' . $this->_context['menuitem']->name;
	}

	protected function _applyParamFilterRule($name)
	{
		return $name . '_' . $this->_context['menuitem']->param;
	}
	
	protected function _applyUsernameFilterRule($name)
	{
		return $name . '_' . $this->_context['identity']->login;
	}
	protected function _applyUserCodeFilterRule($name)
	{
		return $name . '_' . $this->_context['identity']->login;
	}
	protected function _applyTtyFilterRule($name)
	{
		return $name . '_' . $this->_context['identity']->tty;
	}
	protected function _applyHostFilterRule($name)
	{
		return $name . '_webdev';
	}
	protected function _applyMonthFilterRule($name)
	{
		return $name . '_' . date('m', $this->_timestamp);
	}
	protected function _applyDayFilterRule($name)
	{
		return $name . '_' . date('d', $this->_timestamp);
	}
	protected function _applyHourFilterRule($name)
	{
		return $name . '_' . date('H', $this->_timestamp);
	}
	protected function _applyMinuteFilterRule($name)
	{
		return $name . '_' . date('i', $this->_timestamp);
	}
	protected function _applyCurrentDateFilterRule($name)
	{
		return $name . '_' . date('ymd', $this->_timestamp);
	}
	protected function _applyCurrentTimeFilterRule($name)
	{
		return $name . '_' . date('His', $this->_timestamp);
	}
}